package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.ITripArea;
import com.gec.model.system.ITripCity;
import com.gec.model.system.ITripRole;
import com.gec.model.vo.AssginRoleVo;
import com.gec.model.vo.ITripCityQueryVo;
import com.gec.model.vo.ITripRoleQueryVo;
import com.gec.system.common.Result;
import com.gec.system.service.ITripAreaService;
import com.gec.system.service.ITripCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenzhiling
 * @since 2024-01-22
 */
@RestController
@RequestMapping("/system/itrCity")
@Api(tags="城市管理")
@CrossOrigin
public class ITripCityController {

    @Autowired
    private ITripCityService iTripCityService;


    @ApiOperation("获取全部城市列表")

    @GetMapping("/findAll")
    public Result findAll()
    {

        List<ITripCity> list = this.iTripCityService.list();
        return Result.ok(list);
    }




    @ApiOperation("根据id去移除一个角色")
    // 测试删除
    @DeleteMapping("/removeCity/{id}")
    public Result removeCity(@PathVariable Long id)
    {
        boolean b = this.iTripCityService.removeById(id);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 分页和条件查询
    @GetMapping("/{page}/{limit}")
    public Result findCityByPageQuery(@PathVariable Long page,
                                      @PathVariable Long limit,
                                      ITripCityQueryVo iTripCityQueryVo)
    {
        //1.创建分页对象
        IPage<ITripCity> p1 = new Page<>(page,limit);
        //2.调用方法
        p1 =    this.iTripCityService.selectPage(p1,iTripCityQueryVo);
        //3.返回
        return Result.ok(p1);
    }
//   添加城市前获取区域

    @Autowired
    private ITripAreaService iTripAreaService;


    @ApiOperation("获取全部区域列表")

    @GetMapping("/findAllArea")
    public Result findAllArea()
    {
        List<ITripArea> list = this.iTripAreaService.list();
        return Result.ok(list);
    }

    //非主键查询
    @GetMapping("/findCityByAreaId/{areaId}")
    public Result findCityByAreaId(@PathVariable Long areaId)
    {
        List<ITripCity> iTripCity = this.iTripCityService.selectByAreaId(areaId);
        return Result.ok(iTripCity);
    }

    // 添加

    @PostMapping("/addCity")
    public Result addCity(@RequestBody ITripCity iTripCity)
    {
        //        添加编号唯一性判断

        ITripCity it = iTripCityService.selectByName(iTripCity.getCityName());
        if (it != null) {
            return Result.fail("name重复");
        }
        boolean res = this.iTripCityService.save(iTripCity);
        if (res)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 修改

    @GetMapping("/findCityById/{id}")
    public Result findCityById(@PathVariable Long id)
    {
        ITripCity iTripCity = this.iTripCityService.getById(id);
        return Result.ok(iTripCity);
    }



    // 实现修改
    @PostMapping("/updateCity")
    public Result updateCity(@RequestBody ITripCity iTripCity)
    {
        boolean b = this.iTripCityService.updateById(iTripCity);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
    // 批量删除
    @DeleteMapping("/removeCityByIds")
    public Result removeCityByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.iTripCityService.removeByIds(ids);
        if (b)
        {
            return Result.ok();
        }
        else
        {
            return Result.fail();
        }
    }
}

